Skip to content

fix: replace fs.promises.mkdtemp with mkdir for Workers compatibility#160

Open
ebrainte wants to merge 1 commit intocloudflare:mainfrom
ebrainte:fix/replace-mkdtemp-for-workers-compat
Open

fix: replace fs.promises.mkdtemp with mkdir for Workers compatibility#160
ebrainte wants to merge 1 commit intocloudflare:mainfrom
ebrainte:fix/replace-mkdtemp-for-workers-compat

Conversation

@ebrainte
Copy link
Copy Markdown

Summary

  • Replace fs.promises.mkdtemp with fs.promises.mkdir + random suffix in 3 call sites
  • Fixes runtime crash when using @cloudflare/playwright in a Cloudflare Worker

Problem

Cloudflare Workers' nodejs_compat (via unenv) does not implement fs.mkdtemp or fs.promises.mkdtemp. When deploying the Playwright MCP example (or any Worker using @cloudflare/playwright), the Worker crashes at runtime with an error like:

TypeError: fs.promises.mkdtemp is not a function

This affects @cloudflare/playwright@1.1.2 (and likely earlier versions).

Fix

Replace mkdtemp(prefix) with mkdir(prefix + randomSuffix, { recursive: true }) in:

File Line Description
packages/playwright-core/src/server/browserType.ts 158 Artifacts directory creation
packages/playwright-core/src/server/browserType.ts 167 User data directory creation
packages/playwright-core/src/server/chromium/chromium.ts 92 CDP artifacts directory creation

The random suffix uses Math.random().toString(36).slice(2), providing sufficient uniqueness for temporary directory names while using only fs.promises.mkdir which IS available in Workers.

Requires

compatibility_date >= 2025-09-23 in the Worker's wrangler.toml for fs.promises.mkdir to be available.

Testing

Verified by deploying the cloudflare/playwright-mcp example Worker with these changes patched via patch-package. The Worker successfully launches browsers, navigates pages, and takes screenshots without any mkdtemp-related errors.

Cloudflare Workers' nodejs_compat (via unenv) does not implement
fs.mkdtemp or fs.promises.mkdtemp. This causes a runtime crash when
using @cloudflare/playwright in a Worker environment.

Replace mkdtemp(prefix) with mkdir(prefix + randomSuffix, { recursive: true })
in the three call sites within browserType.ts and chromium.ts. The random
suffix uses Math.random().toString(36).slice(2), providing sufficient
uniqueness for temporary directory names.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants